package com.sunthy.mall.controller.client;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.sunthy.mall.model.admin.Result;
import com.sunthy.mall.model.admin.bo.admin.AdminLoginBo;
import com.sunthy.mall.model.admin.bo.client.user.LogoutClientBo;
import com.sunthy.mall.model.admin.bo.client.user.UpdatePwdBo;
import com.sunthy.mall.model.admin.bo.client.user.UpdateUserDataBo;
import com.sunthy.mall.model.admin.vo.admin.AllUserVO;
import com.sunthy.mall.model.admin.vo.client.login.UserLoginVo;
import com.sunthy.mall.model.admin.vo.client.user.UserDataVo;
import com.sunthy.mall.service.admin.user.UserService;
import com.sunthy.mall.service.admin.user.UserServiceImpl;
import com.sunthy.mall.utils.HttpUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 前台
 */
@WebServlet("/api/mall/user/*")
public class UserServlet extends HttpServlet {

    private Gson gson = new Gson();
    private UserService userService = new UserServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        if ("/login".equals(pathInfo)) {
            login(request, response);
        } else if ("/signup".equals(pathInfo)) {
            signup(request, response);
        } else if ("/updatePwd".equals(pathInfo)) {
            updatePwd(request, response);
        }else if ("/updateUserData".equals(pathInfo)) {
            updateUserData(request, response);
        }else if ("/logoutClient".equals(pathInfo)) {
            logoutClient(request, response);
        }
    }

    /**
     * 推出登录的逻辑：
     *      1.获取参数
     *      2.修改数据
     *      3.返回参数
     * @param request
     * @param response
     */
    private void logoutClient(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取请求参数
        String requestBody = HttpUtils.getRequestBody(request);
        LogoutClientBo logoutClientBo = null;
        // 校验参数
        try {
            logoutClientBo = gson.fromJson(requestBody,LogoutClientBo.class);
        } catch (JsonSyntaxException e) {
            response.getWriter().println(gson.toJson(Result.error("参数错误")));
            return;
        }
        // 修改数据
        // todo 为user表设置一个标志为 对登录状态进行判断
        // 返回结果
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 保存修改user资料的逻辑:
     *      1.接收请求参数
     *      2.修改数据
     *      3.返回结果
     * @param request
     * @param response
     */
    private void updateUserData(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取请求参数
        String requestBody = HttpUtils.getRequestBody(request);
        UpdateUserDataBo updateUserDataBo = null;
        // 校验
        try {
            updateUserDataBo = gson.fromJson(requestBody,UpdateUserDataBo.class);
        } catch (JsonSyntaxException e) {
            response.getWriter().println(gson.toJson(Result.error("参数异常")));
        }
        // todo
        // 修改数据
        int code = userService.updateUserData(updateUserDataBo);
        // 返回结果
        if (code==0){
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("保存异常")));
    }

    /**
     * 修改密码的逻辑:
     * 1.接收请求参数
     * 2.修改数据
     * 3.返回结果
     *
     * @param request
     * @param response
     */
    private void updatePwd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取请求参数
        String requestBody = HttpUtils.getRequestBody(request);
        UpdatePwdBo updatePwdBo = null;
        // 校验参数
        try {
            updatePwdBo = gson.fromJson(requestBody, UpdatePwdBo.class);
        } catch (JsonSyntaxException e) {
            response.getWriter().println(gson.toJson(Result.error("参数异常")));
            return;
        }
        if (updatePwdBo.getOldPwd() == null || "".equals(updatePwdBo.getOldPwd())) {
            response.getWriter().println(gson.toJson(Result.error("oldPwd不能为空")));
            return;
        }
        if (updatePwdBo.getNewPwd() == null || "".equals(updatePwdBo.getNewPwd())) {
            response.getWriter().println(gson.toJson(Result.error("newPwd不能为空")));
            return;
        }
        if (updatePwdBo.getConfirmPwd() == null || "".equals(updatePwdBo.getConfirmPwd())) {
            response.getWriter().println(gson.toJson(Result.error("confirmPwd不能为空")));
            return;
        }
        if (!updatePwdBo.getNewPwd().equals(updatePwdBo.getConfirmPwd())) {
            response.getWriter().println(gson.toJson(Result.error("newPwd和confirmPwd不能不一样")));
            return;
        }
        if (updatePwdBo.getOldPwd().equals(updatePwdBo.getNewPwd())) {
            response.getWriter().println(gson.toJson(Result.error("oldPwd不能和newPwd相同")));
            return;
        }
        // 修改数据
        int code = userService.updatePwd(updatePwdBo);
        // 返回结果
        if (code==0){
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }else if (code==404){
            response.getWriter().println(gson.toJson(Result.error("oldPwd错误")));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("修改异常")));
    }

    /**
     * user注册账户的逻辑:
     * 1.接收请求参数
     * 2.查询数据库
     * 3.返回结果
     *
     * @param request
     * @param response
     */
    private void signup(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取请求参数
        String requestBody = HttpUtils.getRequestBody(request);
        // 校验参数
        AllUserVO allUserVO = null;
        try {
            allUserVO = gson.fromJson(requestBody, AllUserVO.class);
        } catch (JsonSyntaxException e) {
            response.getWriter().println(gson.toJson(Result.error("参数异常")));
            return;
        }
        // 邮箱不能为空
        if ("".equals(allUserVO.getEmail())) {
            response.getWriter().println(gson.toJson(Result.error("用户名不能为空")));
            return;
        }// 昵称不能为空
        if ("".equals(allUserVO.getNickname())) {
            response.getWriter().println(gson.toJson(Result.error("昵称不能为空")));
            return;
        }// 密码不能为空
        if ("".equals(allUserVO.getPwd())) {
            response.getWriter().println(gson.toJson(Result.error("密码不能为空")));
            return;
        }// 收件人不能为空
        if ("".equals(allUserVO.getRecipient())) {
            response.getWriter().println(gson.toJson(Result.error("收件人不能为空")));
            return;
        }// 地址不能为空
        if ("".equals(allUserVO.getAddress())) {
            response.getWriter().println(gson.toJson(Result.error("地址不能为空")));
            return;
        }// 手机号不能为空
        if ("".equals(allUserVO.getPhone())) {
            response.getWriter().println(gson.toJson(Result.error("手机号不能为空")));
            return;
        }
        // 添加user
        int code = userService.signUpUser(allUserVO);
        // 返回参数
        if (code == 0) {
            response.getWriter().println(gson.toJson(Result.ok(new UserLoginVo(0, allUserVO.getNickname(),
                    allUserVO.getNickname()))));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("该email已经被注册了")));
    }

    /**
     * user登录前台的逻辑:
     * 1.接收请求参数
     * 2.查询数据库
     * 3.返回结果
     *
     * @param request
     * @param response
     */
    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取请求
        String requestBody = HttpUtils.getRequestBody(request);
        // 校验参数
        AdminLoginBo adminLoginBo = null;
        try {
            adminLoginBo = gson.fromJson(requestBody, AdminLoginBo.class);
        } catch (JsonSyntaxException e) {
            response.getWriter().println(gson.toJson(Result.error("参数异常")));
            return;
        }
        // 用户名不能为空
        if ("".equals(adminLoginBo.getEmail())) {
            response.getWriter().println(gson.toJson(Result.error("用户名不能为空")));
            return;
        }
        // 密码不能为空
        if ("".equals(adminLoginBo.getPwd())) {
            response.getWriter().println(gson.toJson(Result.error("密码不能为空")));
            return;
        }
        // 查询数据库
        String code = userService.userLogin(adminLoginBo);
        // 返回结果
        if (code != null) {
            response.getWriter().println(gson.toJson(Result.ok(new UserLoginVo(0, code, code))));

            return;
        }
        response.getWriter().println(gson.toJson(Result.error("密码错误")));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        if ("/data".equals(pathInfo)) {
            data(request, response);
        }
    }

    /**
     * 获得用户资料的逻辑:
     * 1.接收请求参数
     * 2.获取数据
     * 3.返回结果
     *
     * @param request
     * @param response
     */
    private void data(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取请求参数
        String token = request.getParameter("token");
        // 校验参数
        if (token == null || "".equals(token)) {
            response.getWriter().println(gson.toJson(Result.error("参数错误")));
            return;
        }
        // 获取数据
        UserDataVo userDataVo = userService.getUserData(token);
        // 返回结果
        if (userDataVo == null) {
            response.getWriter().println(gson.toJson(Result.error("数据异常")));
            return;
        }
        response.getWriter().println(gson.toJson(Result.ok(userDataVo)));
    }
}
